package com.ursu.server.handler.validator;

import java.util.logging.Logger;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import com.google.inject.Inject;
import com.google.inject.Provider;
import com.google.inject.Singleton;
import com.gwtplatform.dispatch.server.actionvalidator.ActionValidator;
import com.gwtplatform.dispatch.shared.Action;
import com.gwtplatform.dispatch.shared.ActionException;
import com.gwtplatform.dispatch.shared.Result;

public class LoogedInActionValidator implements ActionValidator {
	
	private static final Logger log = Logger.getLogger("LoggedInActionValidator");
	private final Provider<HttpServletRequest> requestProvider;
	
	@Inject
	  LoogedInActionValidator(final Provider<HttpServletRequest> requestProvider) {
	    this.requestProvider = requestProvider;
	  }

	 @Singleton
	@Override
	public boolean isValid(Action<? extends Result> arg0)
			throws ActionException {
		 boolean result = true;

		    log.warning("LoggedIn Action Validator");

		    HttpSession session = requestProvider.get().getSession();

		    Object authenticated = session.getAttribute("login.authenticated");

		    if (authenticated == null) {
		      log.warning("The User has not logged in.");
		      result = false;
		    }
		 
		    return result;
		  }
}
